package com.ts.main.mini.controller;

import com.ts.main.mini.context.UserContext;
import com.ts.main.mini.dto.UserDto;
import com.ts.main.mini.exception.R;
import com.ts.main.mini.service.UserServiceImpl;
import com.ts.main.mini.vo.UserPolicyVo;
import com.ts.main.mini.vo.UserVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 小程序-用户管理
 * @Author:lk
 * @Date: 2025/5/14  21:21
 */
@AllArgsConstructor
@RestController
@RequestMapping("/mini/user")
@Tag(name = "小程序-用户管理")
public class UserController {

    private final UserServiceImpl userService;


    /**
     * 查询用户信息
     * @return
     */
    @Operation(summary = "查询用户信息")
    @GetMapping("/getUserInfo")
    public R<UserVo> getUserInfo(){
        return R.ok(UserContext.getUser());
    }

    /**
     * 获取用户手机号
     */
    @Operation(summary = "获取用户手机号")
    @GetMapping("/getUserPhone")
    public R<String> getUserPhone(@Schema(description = "code") @RequestParam("code") String code){
        return R.ok(userService.getUserPhone(code));
    }

    /**
     * 获取用户openid
     */
    @Operation(summary = "获取用户openid")
    @GetMapping("/getUserOpenid")
    public R<String> getUserOpenid(@Schema(description = "code") @RequestParam("code") String code){
        return R.ok(userService.getUserOpenid(code));
    }

    /**
     * 登录（手机号）
     * @param phone 手机号
     * @param openid 用户openid
     * @return
     */
    @Operation(summary = "登录（手机号）")
    @GetMapping("/login")
    public R<UserVo> login(@Schema(description = "手机号") @RequestParam String phone,
                           @Schema(description = "用户openid") @RequestParam(required = false) String openid){
        return R.ok(userService.login(phone,openid));
    }

    /**
     * 修改用户信息
     * @param dto
     * @return
     */
    @Operation(summary = "修改用户信息")
    @PutMapping("/updateUserInfo")
    public R<Boolean> updateUserInfo(@RequestBody UserDto dto){
        return R.ok(userService.updateUserInfo(dto));
    }

    /**
     * 退出登录
     * @return
     */
    @Operation(summary = "退出登录")
    @GetMapping("/logout")
    public R<Boolean> logout(){
        return R.ok(userService.logout());
    }

    /**
     * 注销账号
     * @return
     */
    @Operation(summary = "注销账号")
    @GetMapping("/logoff")
    public R<Boolean> logoff(){
        return R.ok(userService.logoff());
    }

    /**
     * 查看用户注册协议和隐私政策
     */
    @Operation(summary = "查看用户注册协议和隐私政策")
    @GetMapping("/getUserPolicy")
    public R<UserPolicyVo> getUserPolicy(){
        return R.ok(userService.getUserPolicy());
    }

}
